﻿Imports System.Windows.Forms

Public Class EditorTabPage
    Inherits TabPage

    Private _firstchange As Boolean = True

    Public Sub New(ByRef name As String, ByRef projectfile As Project.ProjectFile)
        _projectfile = projectfile
    End Sub

    Private _projectfile As Project.ProjectFile
    ReadOnly Property ProjectFile As Project.ProjectFile
        Get
            Return _projectfile
        End Get
    End Property

    ' Return fullname of file open in tab
    ReadOnly Property FullName As String
        Get
            Return _projectfile.FullName
        End Get
    End Property

    Private WithEvents _scintila As ScintillaEditor
    ReadOnly Property ScintillaEditor As ScintillaEditor
        Get
            Return _scintila
        End Get
    End Property

    Public Sub New()
    End Sub

    Public Shadows Sub OnControlAdded(ByVal sender As Object, ByVal e As EventArgs) Handles MyClass.ControlAdded
        _scintila = CType(Controls(0), ScintillaEditor)
    End Sub

    Public Sub OnEditorTextChanged() Handles _scintila.TextChanged
        If _firstchange Then
            ' Do not change the fact that we haven't saved the file
            _firstchange = False
        Else
            _projectfilesaved = False
        End If
    End Sub

    Private _projectfilesaved As Boolean = True
    Public Function ShouldCloseTab() As Boolean

        If Not _projectfilesaved Then
            ' Ask if really want to quit and if want to save project file
            Dim result = MessageBox.Show("Do you want to save before closing?", "Save File?", MessageBoxButtons.YesNoCancel)
            If result = DialogResult.Yes Then
                ' Save File...
                SaveFile()
                Return True
            ElseIf result = DialogResult.No Then
                ' Close it without saving
                Return True
            ElseIf result = DialogResult.Cancel Then
                Return False
            End If

            Return False
        Else
            Return True
        End If

    End Function

    Public Sub SaveFile()
        _projectfile.FileContents = _scintila.Text
        _projectfilesaved = True
    End Sub

    Public Function ShowSaveFileDialog() As DialogResult
        Throw New NotImplementedException()
    End Function


End Class
